﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TestControls
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CheckBox1.OnCheckedChanged += new EventHandler(CheckBox1_OnCheckedChanged);
            DatePicker1.OnSelectedChanged += new EventHandler(DatePicker1_OnSelectedChanged);
            Button3.Click += new EventHandler(Button3_Click);
            if (!Page.IsPostBack)
            {
                menu1.FileXml = Server.MapPath("~/menu.xml");
                TextBox1.EnabledValidator = true;
                TextBox1.ErrorMessage = "Campo requerido";
                TextBox1.Text = "1";
                TextBox2.EnabledValidatorExpression = true;
                TextBox2.EnabledValidator = true;
                TextBox2.ValidationExpression = @"^[0-9a-zA-Z\.\_]+@[0-9a-zA-Z\-]+[\.]{1}[0-9a-zA-Z]+[\.]?[0-9a-zA-Z]+$";
                TextBox2.ErrorMessageExpression = "Correo invalido";
                
                ComboBox1.DataTextField = "NomPais";
                ComboBox1.DataValueField = "IdPais";
                //ComboBox1.AutoPostBack = false;
                ComboBox1.DataSource = getTable();
                ComboBox1.DataBind();

                DatePicker1.MinDate = DateTime.Now;
                //DatePicker1.EnabledValidator = true;
                
                GridView1.AutoPostBack = true;
            }
        }

        void Button3_Click(object sender, EventArgs e)
        {
            ComboBox1.DataBind();
        }

        void DatePicker1_OnSelectedChanged(object sender, EventArgs e)
        {
            //ScriptManager.RegisterClientScriptBlock(Button1, typeof(Page), "a", "alert('" + DatePicker1.Text + "');", true);
        }

        protected void CheckBox1_OnCheckedChanged(object sender, EventArgs e)
        {
            ScriptManager.RegisterClientScriptBlock(Button1, typeof(Page), "a", "alert('Checked_changed');", true);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text == "1")
                ScriptManager.RegisterClientScriptBlock(Button1, typeof(Page), "a", "alert('Hola');", true);
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            GridView1.AllowPaging = false;
            GridView1.DataBind();
            GridView1.ToExportExcel("prueba");
            GridView1.AllowPaging = true;
            GridView1.DataBind();
            //GridView1.ToExportCsv();
            //if (!GridView1.ToExportCsv())
            //{
            //    ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "alert1", "alert('Hubo un problema para exportar')", true);
            //}
        }

        public override void VerifyRenderingInServerForm(Control control)
        {
            /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
               server control at run time. */
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Label1.Text = String.Format("You selected product '{0}'.", GridView1.SelectedDataKey["IdVisita"]);
        }

        protected void GridView1_SelectedIndexChanging(Object sender, GridViewSelectEventArgs e)
        {
            GridViewRow row = GridView1.Rows[e.NewSelectedIndex];

            if (row.Cells[0].Text == "1")
            {
                e.Cancel = true;
                Label1.Text = "You cannot select " + row.Cells[2].Text + ".";
            }
        }

        private DataTable getTable()
        {
            DataTable dt = new DataTable("dt1");
            dt.Columns.Add("IdPais", typeof(int));
            dt.Columns.Add("NomPais", typeof(string));

            dt.Rows.Add(10, "México");
            dt.Rows.Add(20, "Colombia");
            dt.Rows.Add(30, "Chile");
            dt.Rows.Add(40, "Cuba");

            return dt;
        }

    }
}
